/**
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xume.raob.annotations;

import static org.xume.raob.util.ReflectionUtils.isAnObjectBuilder;
import static org.xume.raob.util.ReflectionUtils.targetClass;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

import org.xume.raob.api.BuildStep;
import org.xume.raob.api.BuildStepFactory;
import org.xume.raob.api.InvocationException;
import org.xume.raob.api.ObjectBuilder;
import org.xume.raob.util.ReflectionUtils;

/**
 * @author Johan Siebens
 */
public abstract class AbstractMethodInvocationBuilderStepFactory<A extends Annotation> implements BuildStepFactory<A> {

	protected final BuildStep createBuildStep(Method method, Object... arguments) {
		return new MethodInvocationBuildStep(method, arguments);
	}

	protected final Class<?>[] resolveParameterTypes(Method builderMethod) {
		return resolveParameterTypes(builderMethod.getGenericParameterTypes());
	}

	protected final Class<?>[] resolveParameterTypes(Type[] types) {
		Class<?>[] result = new Class<?>[types.length];
		for (int i = 0; i < types.length; i++) {
			Type type = types[i];
			if (isAnObjectBuilder(type)) {
				result[i] = targetClass(type);
			} else {
				result[i] = (Class<?>) type;
			}
		}
		return result;
	}

	private static class MethodInvocationBuildStep implements BuildStep {

		private Object[] arguments;

		private Method method;

		public MethodInvocationBuildStep(Method method, Object... arguments) {
			this.method = method;
			this.arguments = arguments;
		}

		public void execute(Object target) {
			try {
				method.invoke(target, arguments());
			} catch (InvocationTargetException e) {
				throw new InvocationException(method, e.getTargetException());
			} catch (Exception e) {
				throw new InvocationException(method, e);
			}
		}

		private Object[] arguments() {
			Object[] result = null;
			if (arguments != null) {
				result = new Object[arguments.length];
				for (int i = 0; i < arguments.length; i++) {
					Object current = arguments[i];
					if (current == null || !ReflectionUtils.isAnObjectBuilder(current.getClass())) {
						result[i] = current;
					} else {
						result[i] = ((ObjectBuilder<?>) current).build();
					}
				}
			}
			return result;
		}

	}
}
